// pages/details/details.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
  
    //详情页商品数据
      detailsList:[],
    //购物车中的商品总数
      totalNum:0,
      imgUrl:"../../images/favorite.png",
      text:"收藏",
      collectList:[]

    

  },

  //跳转到首页
  toIndex:function(){
    wx.switchTab({
      url: '/pages/index/index',
    })
  },

//跳转到购物车的点击事件
  toCart:function(){
    wx.switchTab({
      url: '/pages/cart/cart',
    })
  },
  //跳转到愿望单的点击事件
  toFavorite:function(){
    wx.switchTab({
      url: '/pages/favorite/favorite',
    })
  },
  collect:function(){
    let collectList = wx.getStorageSync('collectList')

    let imgUrl = "../../images/favorite.png"
    let text = "收藏"
    let isShow = true
    if(collectList.length == 0){
      //1、收藏夹没有商品，直接收藏
      wx.setStorageSync('collectList', this.data.detailsList)
      imgUrl = "../../images/favorite1.png"
      text = "已收藏"
    }else{
      //2.收藏夹中有商品
      for(let i=0;i<collectList.length;i++){
        if(collectList[i].id == this.data.detailsList[0].id){
          collectList.splice(i,1)
          wx.setStorageSync('collectList', collectList)
          imgUrl = "../../images/favorite.png"
          text = "收藏"
          isShow = false
        }
      }
      //2.2、商品id不同，收藏商品
      if(isShow){
        collectList.unshift(this.data.detailsList[0])
        wx.setStorageSync('collectList', collectList)
        imgUrl = "../../images/favorite1.png"
        text = "已收藏"
      }

    }
    this.setData({
      imgUrl,
      text
    })
  },
  //加入购物车的点击事件

  
  addCar:function(){
    let carList = wx.getStorageSync('carList')
    //console.log(carList.length)
    let num =0
    if(carList.length == 0){
      //1.购物车中没有商品，直接添加商品
      wx.setStorageSync('carList', this.data.detailsList)
      num ++
    }else{
      //2.购物车中有商品
      let isTrue = true
      for(let i = 0; i < carList.length; i++){
        //console.log(carList[i])
        //2.1商品的id相同，数量加一
        if(carList[i].id == this.data.detailsList[0].id){
          //当前商品数量加一
          carList[i].num ++
          //更新缓存数据
          wx.setStorageSync('carList', carList)
          //商品id相同，不执行2.2
          isTrue = false
        }
      }
      //2.2商品的id不同，添加该商品
      if(isTrue){
        //在购物车头部添加数据
        carList.unshift(this.data.detailsList[0])
        //更新缓存数据
        wx.setStorageSync('carList', carList)
      }
      //计算商品总数
      //console.log(carList)
      for(let j=0;j<carList.length;j++){
        num +=carList[j].num
      }

    }
    this.setData({
      totalNum:num
    })
  },
  toIndex:function(){
    wx.switchTab({
      url: '/pages/index/index',
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    let detailsList = wx.getStorageSync('detailsList')
    //获取购物车的缓存数据
    let carList = wx.getStorageSync('carList')
    let collectList = wx.getStorageSync('collectList')
    let imgUrl = "../../images/favorite.png"
    let text = "收藏"
    //定义初始商品数量
    let num = 0
    //当购物车中有商品时计算商品总数
    if(carList.length != 0){
      for(let i=0;i<carList.length;i++){
        num += carList[i].num

      }
    }
    if(collectList.length != 0){
      let isShow = true
    for(let i=0;i<collectList.length;i++){
      if(collectList[i].id == detailsList[0].id){

        imgUrl = "../../images/favorite1.png"
        text = "已收藏"
        isShow = false
      }
    }
    if(isShow){
      imgUrl = "../../images/favorite.png"
      text = "收藏"
    }
  }
      this.setData({
        detailsList:wx.getStorageSync('detailsList'),
        totalNum:num,
        text,
        imgUrl


      })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})